﻿
Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService
Partial Class FrontEnd_Control_Store_Edit
    Inherits System.Web.UI.UserControl
        'Luu nhung MenuId duoc check
    Private IDChecked As String = ""
    Private dtListMenuSelect As List(Of Category)
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If
            LoadType()
            ShowData()
        End If
    End Sub
    Protected Sub LoadType()
        Using data As New VSEntities
            'Khu vuc
            ddlTinhTP.AppendDataBoundItems = True
            ddlTinhTP.Items.Insert(0, New ListItem("---Chọn---", "0"))
            Dim ba = (From ca In data.Provinces Where ca.ParentId = 171 Select ca.Id, ca.Title).ToList
            ddlTinhTP.DataTextField = "Title"
            ddlTinhTP.DataValueField = "ID"
            ddlTinhTP.DataSource = ba
            ddlTinhTP.DataBind()
        End Using
    End Sub

    Protected Sub ShowData()
        Using data As New VSEntities
            Dim _UserId As Integer = Session("UserId")
            '   Dim _StoreId As Integer = (From q In data.Stores Where q.UserId = _UserId And q.StatusId = 3 Select q.StoreId).FirstOrDefault
            '  Dim _StoreId As Integer = (From q In data.Stores Where q.UserId = _UserId Select q.StoreId).FirstOrDefault


            Dim p As Store = (From q In data.Stores Where q.UserId = _UserId Select q).FirstOrDefault
            If Not p Is Nothing Then
                hidID.Value = p.StoreId
                txtTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
                txtDescription.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
                txtContactus.Text = IIf(IsNothing(p.ContactUs) = True, "", p.ContactUs)
                txtUrlweb.Text = IIf(IsNothing(p.URLWEB) = True, "", p.URLWEB)
                txtEmail.Text = IIf(IsNothing(p.Email) = True, "", p.Email)
                hidImagePath.Text = IIf(IsNothing(p.ImgLogo) = True, "", p.ImgLogo)
                imgUpload.ImageUrl = IIf(IsNothing(p.ImgLogo) = True, "", p.ImgLogo)

                txtSkypeid.Text = IIf(IsNothing(p.SkypeId) = True, "", p.SkypeId)
                txtSlogan.Text = IIf(IsNothing(p.Slogan) = True, "", p.Slogan)
                txtYahooid.Text = IIf(IsNothing(p.YahooId) = True, "", p.YahooId)

                ddlTinhTP.SelectedValue = IIf(IsNothing(p.TinhThanhId) = True, "0", p.TinhThanhId)
                txtDiachi.Text = IIf(IsNothing(p.DiaChi) = True, "", p.DiaChi)
                txtHoTen.Text = IIf(IsNothing(p.HoTen) = True, "", p.HoTen)
                txtPhone.Text = IIf(IsNothing(p.Phone) = True, "", p.Phone)
                txtPhoneOrther.Text = IIf(IsNothing(p.PhoneOther) = True, "", p.PhoneOther)

                Dim strImage As String = ""
                Dim t = (From a In data.StoreImages Where a.StoreId = p.StoreId Select a.StoreId, a.ImageURL, a.Description).ToList
                If Not t Is Nothing Then
                    For i As Integer = 0 To t.Count - 1
                        strImage += "showImage('" + i.ToString + "','" + t.Item(i).ImageURL.ToString + "','" + t.Item(i).Description.ToString + "');"
                    Next
                End If
                Excute_Javascript(strImage, Me.Page, True)
            End If
        End Using
    End Sub
#End Region
#Region "Event for control"
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim arrImage() As String = hidStrImage.Text.Trim.Split(",")
        Dim arrDest() As String = hidStrCommnet.Text.Trim.Split(";")
        Using data As New VSEntities
            Dim p As Store = (From q In data.Stores Where q.StoreId = hidID.Value).SingleOrDefault
            Try
                p.StoreId = hidID.Value
                p.Title = txtTitle.Text.Trim()
                p.QueryString = makeURLFriendly(txtTitle.Text.Trim)
                p.Description = txtDescription.Text.Trim()

                p.URLWEB = txtUrlweb.Text.Trim()
                p.Email = txtEmail.Text.Trim()
                p.ImgLogo = hidImagePath.Text.Trim

                p.SkypeId = txtSkypeid.Text.Trim()
                p.Slogan = txtSlogan.Text.Trim()
                p.YahooId = txtYahooid.Text.Trim()
                p.TinhThanhId = ddlTinhTP.SelectedValue
                p.DiaChi = txtDiachi.Text.Trim
                p.HoTen = txtHoTen.Text.Trim
                p.Phone = txtPhone.Text.Trim
                p.PhoneOther = txtPhoneOrther.Text.Trim

                data.SaveChanges()
                If p.StoreId > 0 Then
                    Dim t As List(Of StoreImage) = (From h In data.StoreImages Where h.StoreId = hidID.Value Select h).ToList
                    For Each h As StoreImage In t
                        data.StoreImages.DeleteObject(h)
                    Next
                    For i As Integer = 0 To arrImage.Length - 2
                        Dim b As New StoreImage
                        b.Description = arrDest(i).ToString
                        b.ImageURL = arrImage(i).ToString
                        b.StoreId = p.StoreId
                        data.StoreImages.AddObject(b)
                    Next
                End If
                p.ContactUs = txtContactus.Text.Trim()
                data.SaveChanges()
                Insert_App_Log("Update Store:" & txtTitle.Text.Trim & "", Function_Name.Store, Audit_Type.Create, Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("Alertbox('Cập nhật dữ liệu thành công.');window.location ='chinh-sua-gian-hang.html';", Me.Page, True)

            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            End Try
        End Using
    End Sub
    Protected Sub btnHuy_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnHuy.Click
        Response.Redirect(MainDomain & "chinh-sua-gian-hang.html")
    End Sub
#End Region
End Class
